<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\JsonResponse;
use Illuminate\Http\Response;
use App\Models\Admin;

class TestController extends Controller {
    public function insert(){
        $admin = array(
            'name'  => '胡飞1号',
            'username'  => 'hufei1995',
            'password'  => '123456',
            'salt'      => '',
            'token'     => '',
//            'login_time'=> date("Y-m-d H:i:s" ,strtotime('-1day'))    //也可以
            'login_time'=> date("Y-m-d H:i:s")
        );
        $id = DB::table('admin')->insertGetId($admin);
        return new Response($id);
//        return view();
    }
    
    public function select(){
        $admins = DB::table('admin')->get();
        $admin = array();
        foreach ($admins as $key=>$value){
            $admin[$key] = $value;
        }
        return new JsonResponse($admin);
    }
    
    public function select_1(){
        $where=array();
        $where[] = ['id','>',6];
        $where[] = ['name','hufei'];
        $admins = DB::table('admin')->where($where)->get();
        $admin = array();
        foreach ($admins as $key=>$value){
            $admin[$key] = $value;
        }
        return new JsonResponse($admin);
    }
    
    public function select_2(){
//        DB::table('admin')
        $admin = Admin::find(1);
        return response()->json($admin);
    }
    
    public function sum(){
        $admin = Admin::sum('id');
        return response()->json($admin);
    }
}

